package games.padinvaders;

import utils.RandomNumber;
import games.Game;
import hardware.Display2D;
import hardware.UI;

public class PadInvaders extends Game{

private static Defender defender;	
private static InvadersSquad myInvadersSquad;
private static DefenderProjectile myDefenderProjectile;
private static InvaderProjectile myInvaderProjectile;

private static int lives;





public PadInvaders()
{
	gameID=1;
	gameName= "Invaders";	
	highestSelectableLevel=9;
	
	myDefenderProjectile =new DefenderProjectile();
	defender = new Defender();	
	
	
	myInvaderProjectile =new InvaderProjectile();
	myInvadersSquad = new 	InvadersSquad(myInvaderProjectile);
	
	
	
}




public int demo() throws InterruptedException
{
	int cmd;
	do{	
		cmd=displayGameName();
		if (cmd==CMD_NONE)
			{
			cmd=playDemo();
			
			if (cmd==CMD_NONE)
				{
				cmd=displayHighscores();		
				}
			}
		
			if (cmd==CMD_SETUP)
			{
				cmd=configGame();	
			}
	}
	while (cmd==CMD_NONE);
	
return(cmd);		
}





public int configGame() throws InterruptedException
{
	int cmd=CMD_NONE;
	int menue=0;
	
	while (true)
		{
		switch (menue)
			{
			case 0:
				cmd=selectLevel();	
			break;

					
			case 1:
				//cmd=setupAcc();
				break;
			}
	
		if (cmd==CMD_NEXT)
			menue++;

		if (cmd==CMD_PREV)
			menue--;

		
		if (menue<0)
			menue=1;

		if (menue>2)
			menue=0;

		
		if ((cmd==CMD_SELECT) || (cmd==CMD_ABORT))
				return(cmd);
		}	
}






public int playDemo() throws InterruptedException 
{
	prepareLevel(1);
	
	int cmd;
	int invaderCounter;
	
	int invadersEnergy;
	int shotCost;
	
	shotCost=20;
	invaderCounter=0;
	invadersEnergy=0;
	
	while(true)
	{
	
		Display2D.clear();
		
//	if (defenderCounter==0)
		{
		//myDefender.moveDefender();
		}
	

		myInvadersSquad.move();
	
	 
	if (myInvaderProjectile.isVisible())
		{
		myInvaderProjectile.move();
		}
	
	if ((invadersEnergy>shotCost) && (!myInvaderProjectile.isVisible()))
		{
		if ((RandomNumber.get() % 0x02) ==1)
			{
		//	myInvadersSquad.fire(myInvaderProjectile,InvaderProjectile.PROJECTILE_TYPE_RANDOM);
			}
			
		invadersEnergy-=shotCost;
		}
	
	myInvaderProjectile.draw();
	myInvadersSquad.draw();
	
	invaderCounter++;
	
	if (invaderCounter>11)
		{
		invaderCounter=0;
		}
	
	
	
	Display2D.doRepaint();
	
	
	invadersEnergy++;
	
	cmd=checkKeys();
	if (cmd!=CMD_NONE)
		return(cmd);
	
	
	Thread.sleep(60);
	}
}
	



private void prepareLevel(int level) throws InterruptedException
{
	int invadersShootCost;
	int invadersSpeed;
	
	invadersShootCost=100-(level*5);
	if (invadersShootCost<10)
		invadersShootCost=10;
	
	invadersSpeed=30-(level*2);
	if (invadersSpeed<10)
		invadersSpeed=10;
	
	myInvadersSquad.arive();
	defender.init(200,20,defender.getLifePoints()+20);
	myInvadersSquad.init(400, invadersSpeed,level,1,invadersShootCost);
	myInvaderProjectile.setVisible(false);
	
	
	while(!myInvadersSquad.arrived())
		{
		Display2D.clear();
		
		defender.draw();
		myInvadersSquad.draw();
	
		Display2D.doRepaint();
		

			Thread.sleep(10);

		}
		
}






public void play() throws InterruptedException
{
	
	actualLevel=beginLevel;
	defender.setLifePoints(0);
	
	while(true)
		{
			{
			displayLevelNumber();
			if (playLevel(actualLevel)==true)
				{
				levelWin();
				actualLevel++;
				}
			else
				{
				levelLoose();
				actualLevel=1;
				defender.setLifePoints(0);
				return;
				}
			}
	
		if (UI.keyLDown())
			return;
		
		}
	
}
	




private void levelWin() throws InterruptedException
{

	while(defender.yPos<Display2D.getRowCount()-1)
	{

	
	Display2D.clear();
	
	defender.draw();
	
	
	Display2D.doRepaint();

	if (defender.xPos>(Display2D.getColumnCount()/2))
		{
		defender.xPos--;
		}
	else if (defender.xPos<(Display2D.getColumnCount()/2))
		{
		defender.xPos++;
		} 

	
	defender.yPos++;

	Thread.sleep(200);
	}	

}




private void levelLoose() throws InterruptedException
{
	int counter;
	
	counter=0;
	
	while(myInvadersSquad.getMinYPos()>0)
	{

	Display2D.clear();

	if (counter==0)
		{
		myInvadersSquad.descent();
		}
	
	counter++;
	
	if (counter>10)
		{
		counter=0;
		}
	
	myInvadersSquad.draw();
	
	Display2D.doRepaint();

	
	Thread.sleep(10);
	}
	
}



public boolean playLevel(int level) throws InterruptedException
{

	
	prepareLevel(level);
	



	
	lives=3;
	

	while (true)
	{
		
		Display2D.clear();


		defender.setMoveVector();


		int projectileType;
		
		
		
		if (UI.keyRUp())
				projectileType=DefenderProjectile.TYPE_LASER;
		else if (UI.keyRUp())
			projectileType=DefenderProjectile.TYPE_MISSLE;
		else
			projectileType=DefenderProjectile.TYPE_NONE;
		
		
		if ((projectileType!=DefenderProjectile.TYPE_NONE) & (!myDefenderProjectile.isVisible()))
			defender.shoot(myDefenderProjectile,projectileType);

		
		
		// Move Objects
		
		
		defender.move();
		myDefenderProjectile.move();
		myInvaderProjectile.move();
		myInvadersSquad.movieSquad();
		
		
		// Check Collisions
		
		

		myInvadersSquad.checkEnemyFire(myDefenderProjectile);
		
		defender.damage(myInvadersSquad.checkCollision(defender.xPos,defender.yPos));
		
		defender.damage(myInvaderProjectile);
		
		
				
		// charge Energy
		defender.charge(1);
		myInvadersSquad.charge();


		// Draw Objects
		

		
		myDefenderProjectile.draw();
		myInvaderProjectile.draw();

		defender.draw();
		myInvadersSquad.draw();

		
		Display2D.doRepaint();
		
		
		
		// check exit conditions 
		
		if (!myInvadersSquad.somebodyVisible())
			{
			return(true);
			}

	
	if (myInvadersSquad.getMinYPos()==(0) ||
			(!defender.isAlive()))
			//(!defender.isVisible()))
		{
		return(false);
		}
		
		Thread.sleep(1);	

		
	}
	
	
}
	
	
}
